<template>
  <div class="main">
    <div class="header">
      <HeaderBack>论坛</HeaderBack>
    </div>
    <div class="content">
      <scroller ref="myscroller" :on-infinite="infinite" :on-refresh="refresh">
        <BbsListIndex ref="bbs_list" :id="class_id"/>
      </scroller>
      <router-link tag="div" :to="{ path: '/bbs/add', query: { id: this.class_id } }" class="add">发帖</router-link>
    </div>
  </div>
</template>

<script>
import HeaderBack from "@/components/common/HeaderBack.vue";
import BbsListIndex from "@/components/home/BbsListIndex.vue";

export default {
  data() {
    return {
      type: "bbs_list_index"
    };
  },
  components: {
    HeaderBack,
    BbsListIndex
  },
  computed: {
    class_id() {
      return this.$route.query.id;
    }
  },
  methods: {
    refresh() {
      console.log("refresh");
      this.$refs.bbs_list
        .refresh()
        .then(() => {
          this.$refs.myscroller.finishPullToRefresh();
        })
        .catch(() => {
          this.$refs.myscroller.finishPullToRefresh();
          this.$refs.myscroller.finishInfinite(true);
        });
    },
    infinite(done) {
      console.log("infinite");
      this.$refs.bbs_list
        .loadMore()
        .then(() => {
          this.$refs.myscroller.finishInfinite(false);
        })
        .catch(() => {
          this.$refs.myscroller.finishInfinite(true);
        });
    }
  },
  created() {
    console.log(111);
    // setTimeout(() => {
    // this.$refs.bbs_list.refresh();

    // }, 1000);
  },

  beforeRouteEnter(to, from, next) {
    // 进入
    if (!sessionStorage.askPositon) {
      sessionStorage.askPositon = "{}";
    }
    let askPositon = JSON.parse(sessionStorage.askPositon);
    if (!askPositon[to.query.type]) {
      askPositon[to.query.type] = 0;
      sessionStorage.askPositon = JSON.stringify(askPositon);
    }
    next(vm => {
      if (from.path == "/bbs") {
        askPositon[vm.type] = 0;
        sessionStorage.askPositon = JSON.stringify(askPositon);
        vm.$refs.bbs_list.init();
        vm.$refs.bbs_list.refresh().
        then(() => {
          vm.$refs.myscroller.finishPullToRefresh();
        }).
        catch(() => {
          vm.$refs.myscroller.finishInfinite(true);
        });
      }
      console.log("beforeRouteEnter", askPositon[vm.type]);
      // vm.$refs.bbs_list.init();
      // vm.$refs.bbs_list.refresh().then();
      setTimeout(function() {
        vm.$refs.myscroller.scrollTo(0, askPositon[vm.type]);
      }, 0);
    });
  },
  beforeRouteLeave(to, from, next) {
    // 离开
    let askPositon = JSON.parse(sessionStorage.askPositon);
    askPositon[this.type] =
      this.$refs.myscroller && this.$refs.myscroller.getPosition().top;
    sessionStorage.askPositon = JSON.stringify(askPositon);
    next();
  },
  beforeRouteUpdate(to, from, next) {
    let askPositon = JSON.parse(sessionStorage.askPositon);
    // 离开的时候记录滚动条位置
    askPositon[this.type] =
      this.$refs.myscroller && this.$refs.myscroller.getPosition().top;
    sessionStorage.askPositon = JSON.stringify(askPositon);
    // 进入的时候设置滚动条位置

    if (!askPositon[to.query.type]) {
      askPositon[to.query.type] = 0;
      sessionStorage.askPositon = JSON.stringify(askPositon);
    }

    this.$refs.myscroller.scrollTo(0, askPositon[to.query.type]); //askPositon[vm.type];

    next();
  }
};
</script>

<style scoped lang="less">
.content {
  position: relative;
  .add {
    position: absolute;
    bottom: 2rem;
    right: 1rem;
    width: 2.5rem;
    line-height: 2.5rem;
    text-align: center;
    background: #00c281;
    box-shadow: 0 0 .5rem #003624;
    color: #FFFFFF;
    border-radius: 50%;
  }
}
</style>
